// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import iView from 'iview'
import App from './App'
import router from './router'
import VueHighcharts from 'vue-highcharts'
import VueLocalStorage  from 'vue-localstorage'
import Cookies from 'js-cookie'
import axios from './api/middleware'
import EasyScroll from 'easyscrolldt' 
import user from './utils/user'  
Vue.prototype.IFRAME_URL = process.env.IFRAME_URL;


Vue.config.productionTip = false

Vue.use(iView);
Vue.use(VueRouter);
Vue.use(VueHighcharts); 
Vue.use(EasyScroll); 

const LOGIN_PAGE_NAME = 'login' 

// 全局导航钩子
router.beforeEach((to, from, next) => {
    // 判断该路由是否需要登录权限
    let token = user.getToken();
    if(!token && to.name !== LOGIN_PAGE_NAME){  //未登录且跳转页面不是登录页面
      next({
        name: LOGIN_PAGE_NAME // 跳转到登录页
      }) 
    }else if(!token && to.name === LOGIN_PAGE_NAME){  //未登录且跳转页面是登录页面
      next() // 跳转
    }else if(token && to.name === LOGIN_PAGE_NAME){ //已登录且跳转页面是登录页面
      console.log('login skip')
      next({
        name: 'customer'
      }) // 跳转首页
    }else{
      next() // 跳转
    }  
 });

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
